package metrics;

import edu.uci.ics.jung.graph.UndirectedGraph;
import plots.Edge;
import plots.Plot;
import base.Environment;

import static base.Environment.Configurations.*;

/*
 * Computes the destruction of the environment. The main idea is that an environment will be able to
 * 	regenerate faster if it has been evenly harvested and also not fully harvested;
 * Each plot requires a certain percentage to not be harvested as to fully regenerate its resources. 
 */
public class EnvironmentDamage extends EnvironmentMetric {	
		
	public EnvironmentDamage(Environment env){
		super(env);		
	}
	
	protected double computeScore(){
		double result = 0d;		
		double leftOver = 0d;		
		
		UndirectedGraph<Plot, Edge> plots = env.getPlots();
		double regenThr = env.getDouble(REGEN_THRESHOLD);
		double landState = 0d;
		
		for(Plot x:plots.getVertices()) {			
			leftOver = x.getYield()/x.getMaxYield();
			if (leftOver > regenThr)
				leftOver = regenThr;
			landState += (leftOver/regenThr);
		}
		
		result = (1 - landState / plots.getVertexCount())*100d;
		
		return Double.isNaN(result) ? 0D : result;
	}	
	
	@Override
	public String toString(){
		String result= "@ Environment Damage: " + computeScore() +"%";
		
		return result;
	}
	
	
	
}
